﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StudentGrade.Model;
using System.IO;

namespace StudentGrade
{
    class Program
    {
        
        static void Main(string[] args)
        {
            //初始化总成绩，平均成绩，最高成绩，最低成绩,学生集合
            int sum = 0;
            double averagegrade = 0;
            int topgrade = int.MinValue;
            int top = 0;
            int bottom = 0;
            int bottomgrade = int.MaxValue;
            List<Student> students = new List<Student>();
            //寻找txt文件，读取全文并逐步分解为行和单个词
            string path = "D:/VS/StudentGrade/StudentInfor1.txt";
            var text = File.ReadAllText(path);
            string[] line = text.Split('\n');
            string[][] word = new string[line.Length-1][];
            //将对应的数据加入到students集合中
            for(int i=1;i<line.Length;i++)
            {
                word[i - 1] = line[i].Split(' ');
                //电话号码合法性检查
                word[i-1][3]=word[i - 1][3].Trim();
                if (word[i - 1][3].Length != 11)
                    Console.WriteLine($"第{i}个同学电话格式错误"+word[i-1][3]);
                //出生日期合法化检查
                DateTime bir;
                if (!DateTime.TryParse(word[i - 1][4], out bir))
                    Console.WriteLine($"第{i}个学生的出生日期不合法");
                students.Add(new Student(word[i - 1][0],word[i - 1][1],Convert.ToInt32(word[i - 1][2]),word[i - 1][3],word[i-1][4]));
            }
            //计算平均分和最高最低分并记录最高最低分的信息
            for(int i=0;i<students.Count;i++)
            {
                sum += students[i].Grade;
                bottom = students[i].Grade < bottomgrade ? i : bottom;
                top = students[i].Grade > topgrade ? i : top;
                topgrade = Math.Max(topgrade, students[i].Grade);
                bottomgrade = Math.Min(bottomgrade, students[i].Grade);
            }
            averagegrade = Convert.ToDouble(sum / students.Count);
            //输出平均分，最高分，最低分
            Console.WriteLine("班级平均分是");
            Console.WriteLine(averagegrade);
            Console.WriteLine("最高分学生的信息是");
            Console.WriteLine("姓名：{0} 学号：{1} 成绩：{2} 电话：{3} 出生日期：{4}", students[top].Name, students[top].Number, students[top].Grade, students[top].Mobile, students[top].Birth);
            Console.WriteLine("最低分学生的信息是");
            Console.WriteLine("姓名：{0} 学号：{1} 成绩：{2} 电话：{3} 出生日期：{4}", students[bottom].Name, students[bottom].Number, students[bottom].Grade, students[bottom].Mobile, students[bottom].Birth);

            //for(int i=0;i<word.Length/4;i++)
            //{
            //    student1.name = word[4 * i];
            //    student1.number = word[4 * i + 1];
            //    student1.grade = Convert.ToInt32(word[4 * i + 2]);
            //    student1.mobile = word[4 * i + 3];
            //    studens.Add(student1);
            //}
            //foreach (var grade in studens)
            //    Console.Write(grade + " ");
            Console.ReadKey();
        }
    }
}
